package com.wyait.manage.service.impl;

import com.wyait.manage.entity.Internship;
import com.wyait.manage.entity.Work;
import com.wyait.manage.service.InternshipService;
import com.wyait.manage.service.WorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @author Honyelchak
 * @ClassName: WorkComputeService
 * @Description:
 * @create 2020-05-14
 */
@Service
@Slf4j
public class WorkComputeService {

    @Autowired
    WorkService workService;

    @Autowired
    InternshipService internshipService;

    public void compute() {
        // 课程工作量
        List<Work> works = workService.getAll();
        double workLoad = 0;
        String expression;
        for (Work work : works) {
            double k = calcClassK(work);
            Integer practicalHours = work.getPracticalHours();
            Integer theoreticalHours = work.getTheoreticalHours();

            workLoad = k * theoreticalHours;
            work.setWorkload1(workLoad);
            BigDecimal bigDecimal1 = new BigDecimal(workLoad);
            expression = theoreticalHours + "*(" + new BigDecimal(k).setScale(2, RoundingMode.HALF_UP).toString() + ") = " +  bigDecimal1.setScale(2, RoundingMode.HALF_UP).toString();
            if (work.getCourseType().equals("核心课")) expression += "（核心课）";
            work.setExpression1(expression);

            workLoad = k * practicalHours;
            work.setWorkload2(workLoad);
            BigDecimal bigDecimal2 = new BigDecimal(workLoad);
            expression = practicalHours + "*(" + new BigDecimal(k).setScale(2, RoundingMode.HALF_UP).toString() + ") = " + bigDecimal2.setScale(2, RoundingMode.HALF_UP).toString();
            if (work.getCourseType().equals("核心课")) expression += "（核心课）";
            work.setExpression2(expression);

            workService.update(work);
        }


        // 实习工作量
        List<Internship> internships = internshipService.getAll();
        for (Internship internship : internships) {
            workLoad = calcInternShipWorkLoad(internship);
            internship.setWorkLoad(workLoad);

            internshipService.update(internship);
        }
    }

    private double calcClassK(Work work) {
        double k = 0;
        String classes = work.getClasses();
        int stuNum = work.getStuNum();


        String[] split = classes.split(" ");

        int classCount = split.length;

        if (classCount <= 1) {
            if (stuNum < 40) {
                k = 1;
            } else if (stuNum >= 40 && stuNum <= 44) {
                k = 1.1;
            }
        } else if (classCount <= 5) {

            k = 1 + 0.1 * classCount;

            if (stuNum > classCount * 30 + 15) {
                classCount++;
                k += 0.1;
            }
        } else {
            k = 1.5;
        }

        if (work.getCourseType().equals("核心课")) {
            k += 0.1;
        }

        if (classes.indexOf("绍") > 0) {
            k *= 2;
        }
        return k;
    }

    private double calcInternShipWorkLoad(Internship internship) {


        double k = 0;
        String classes = internship.getClasses();
        int week = internship.getWeek();
        int day = week * 5;


        String[] split = classes.split(" ");

        int classCount = split.length;
        k = day * 2 * classCount;

        return k;
    }

}
